package com.ykdl.member.kid.messagebox;

import java.util.ArrayList;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.adapters.TopUserAdapter;
import com.ykdl.member.kid.beans.Memberlistbean;
import com.ykdl.member.kid.beans.UserActorBean;
import com.ykdl.member.kid.circle.MemberListActivity;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.widget.AsyncImageView;
import com.ykdl.member.views.PullToRefreshView;
import com.ykdl.member.views.PullToRefreshView.OnFooterRefreshListener;
import com.ykdl.member.views.PullToRefreshView.OnHeaderRefreshListener;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class TheTopListActivity extends BaseScreen implements OnHeaderRefreshListener,
OnFooterRefreshListener,OnItemClickListener ,OnClickListener{
	private PullToRefreshView mPullToRefreshView;
	private GridView mGridView;
	private TopUserAdapter adapter;
	private Button darenbang_bt,xinrenbang_bt;
	private String TAG = "TheTopListActivity";
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.thetoplistactivity_xml);
		inint();
		}
	public void inint() {
		mPullToRefreshView = (PullToRefreshView) findViewById(R.id.pullToResreshView);
		mPullToRefreshView.setOnHeaderRefreshListener(this);
		mPullToRefreshView.setOnFooterRefreshListener(this);
		darenbang_bt = (Button)findViewById(R.id.darenbang_bt);
		xinrenbang_bt = (Button)findViewById(R.id.xinrenbang_bt);
		darenbang_bt.setOnClickListener(this);
		xinrenbang_bt.setOnClickListener(this);
		findViewById(R.id.main_left).setOnClickListener(this);
		darenbang_bt.setSelected(true);
		adapter =  new TopUserAdapter(this,TAG);
		mGridView = (GridView) findViewById(R.id.picture);
		mGridView.setOnItemClickListener(this);
		getTopUser(true);
	}
	public void getTopUser(boolean isUserTop) {
		showProgress("正在加载中,请稍等...");
		String  pageurl ;
		if (isUserTop) {
			  pageurl =KidConfig.GET_TOPUSERS_LIST;
		}else {
			pageurl =KidConfig.GET_NEWUSERS_LIST;
		}
		
		HttpBaseRequest request  =  Wxxr.getInstance().getBaseRequest(pageurl);
		 TaskManager.startHttpRequest(request, new getTopUserTag(),  Memberlistbean.class);
	}
	
	class getTopUserTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			if (t instanceof Memberlistbean) {
				finishProgress();
				Memberlistbean bean = (Memberlistbean)t;
				adapter.setList(bean.getList());
				adapter.notifyDataSetChanged();
				mGridView.setAdapter(adapter);
				Toast.makeText(mContext, "成功", 1).show();
				}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onHeaderRefresh(PullToRefreshView view) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.darenbang_bt:
			setSelectedID(R.id.darenbang_bt);
			getTopUser(true);
			break;
		case R.id.xinrenbang_bt:
			setSelectedID(R.id.xinrenbang_bt);
			getTopUser(false);
			break;
		case R.id.main_left:
			this.finish();
			break;
		default:
			break;
		}
	}

	public void setSelectedID(int id) {

		darenbang_bt.setSelected(false);
		xinrenbang_bt.setSelected(false);

		if (id == R.id.darenbang_bt) {
			darenbang_bt.setSelected(true);
		} else if (id == R.id.xinrenbang_bt) {
			xinrenbang_bt.setSelected(true);
		}

	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().setCurrentActivity(TAG);
		super.onResume();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().pause(TAG);
		super.onPause();
	}
}
